/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import sosrest.Calendarios;
import sosrest.Citas;
import sosrest.Usuarios;

/**
 *
 * @author pablomosquera
 */
@Stateless
@Path("usuarios")
public class UsuariosFacadeREST extends AbstractFacade<Usuarios> {
    @PersistenceContext(unitName = "sosrestPU")
    private EntityManager em;

    public UsuariosFacadeREST() {
        super(Usuarios.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Usuarios entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Usuarios entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Usuarios find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Usuarios> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Usuarios> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }
    
    @GET
    @Path("{idUsuario}/citas/count")
    @Produces("text/plain")
    public String countEvents(@PathParam("idUsuario") int id, @QueryParam("from") @DefaultValue("0") String fromDate,@QueryParam("to") @DefaultValue("0") String toDate) {
    Collection<Calendarios> col = super.find(id).getCalendariosCollection();
    Collection<Citas> coleve = new ArrayList<Citas>();
    
    for (Iterator<Calendarios> it = col.iterator(); it.hasNext();) {
        Calendarios calendar = it.next();
            //coleve.addAll(calendar.getEventCollection());
            coleve.addAll(calendar.getCitasCollection());
        }
        if(fromDate.equals("0") || toDate.equals("0")){
         return String.valueOf(coleve.size());}
        
        Collection<Citas> colresult = new ArrayList<Citas>();
        for (Iterator<Citas> it = coleve.iterator(); it.hasNext();) {
        try {
                Citas event = it.next();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Date from = df.parse(fromDate);
                Date to = df.parse(toDate); 
         if (event.getFecha().after(from) && event.getFecha().before(to)) {
                     colresult.add(event);
                }
            } catch (ParseException ex) {}
        }
     int cont = colresult.size();
     return String.valueOf(cont);
    }
    
    
    @GET
    @Path("{idUsuario}/citas")
    @Produces({"application/xml","application/json","text/plain"})
    public Collection<Citas> EventsByUsuario(@PathParam("idUsuario") int id, @QueryParam("from") @DefaultValue("0") String fromDate,@QueryParam("to") @DefaultValue("0") String toDate, @QueryParam("idCalendar") @DefaultValue("-1") String idCalendar) {
    Collection<Calendarios> col = super.find(id).getCalendariosCollection();
    Collection<Citas> coleve = new ArrayList<Citas>();
    int idcalendar = Integer.parseInt(idCalendar);
    
    for (Iterator<Calendarios> it = col.iterator(); it.hasNext();) {
        Calendarios calendar = it.next();
        
            if(idCalendar.equals("-1")){
            coleve.addAll(calendar.getCitasCollection());
            }
            if(idcalendar == calendar.getIdCalendario())
            {
            coleve.addAll(calendar.getCitasCollection());
            }
            
        }
        if(fromDate.equals("0") || toDate.equals("0")){
         return coleve;}
        
        Collection<Citas> colresult = new ArrayList<Citas>();
        for (Iterator<Citas> it = coleve.iterator(); it.hasNext();) {
        try {
                Citas event = it.next();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Date from = df.parse(fromDate);
                Date to = df.parse(toDate); 
         if (event.getFecha().after(from) && event.getFecha().before(to)) {
                     colresult.add(event);
                }
            } catch (ParseException ex) {}
        }
     return colresult;
    }
    
    
    
    @GET
    @Path("{idUsuario}/calendarios/publicos/citas/")
    @Produces({"application/xml","application/json","text/plain"})
    public Collection<Citas> publicEventsByUsuario(@PathParam("idUsuario") int id, @QueryParam("from") @DefaultValue("0") String fromDate,@QueryParam("to") @DefaultValue("0") String toDate, @QueryParam("idCalendar") @DefaultValue("-1") String idCalendar) {
    Collection<Calendarios> col = super.find(id).getCalendariosCollection();
    Collection<Citas> coleve = new ArrayList<Citas>();
    int idcalendar = Integer.parseInt(idCalendar);
    
    for (Iterator<Calendarios> it = col.iterator(); it.hasNext();) {
        Calendarios calendar = it.next();
        
        if(calendar.getTipo()== true && idCalendar.equals("-1")){
        coleve.addAll(calendar.getCitasCollection());
        }
        if(calendar.getTipo()== true && idcalendar == calendar.getIdCalendario())
            {
            coleve.addAll(calendar.getCitasCollection());
            }

            
        }
        if(fromDate.equals("0") || toDate.equals("0")){
         
            return coleve;}
        
        Collection<Citas> colresult = new ArrayList<Citas>();
        for (Iterator<Citas> it = coleve.iterator(); it.hasNext();) {
        try {
                Citas event = it.next();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Date from = df.parse(fromDate);
                Date to = df.parse(toDate); 
         if (event.getFecha().after(from) && event.getFecha().before(to)) {
                     colresult.add(event);
                }
            } catch (ParseException ex) {}
        }
     return colresult;
    }
    
    
    
    @GET
    @Path("{idUsuario}/calendarios/publicos/citas/count")
    @Produces("text/plain")
    public String publicEventsByUsuariocount(@PathParam("idUsuario") int id, @QueryParam("from") @DefaultValue("0") String fromDate,@QueryParam("to") @DefaultValue("0") String toDate) {
    Collection<Calendarios> col = super.find(id).getCalendariosCollection();
    Collection<Citas> coleve = new ArrayList<Citas>();
    
    for (Iterator<Calendarios> it = col.iterator(); it.hasNext();) {
        Calendarios calendar = it.next();
        
        if(calendar.getTipo()== true){
        coleve.addAll(calendar.getCitasCollection());
        }

            
        }
        if(fromDate.equals("0") || toDate.equals("0")){
         
            return String.valueOf(coleve.size());}
        
        Collection<Citas> colresult = new ArrayList<Citas>();
        for (Iterator<Citas> it = coleve.iterator(); it.hasNext();) {
        try {
                Citas event = it.next();
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Date from = df.parse(fromDate);
                Date to = df.parse(toDate); 
         if (event.getFecha().after(from) && event.getFecha().before(to)) {
                     colresult.add(event);
                }
            } catch (ParseException ex) {}
        }
     return String.valueOf(colresult.size());
    }
    

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
